<?php
$conn = new pdo('mysql:host=127.0.0.1;port=3306;dbname=zgdb;charset=utf8', 'root', '', [19 => 2, 1002 => 'set names utf8']);
//分页实现
//(1)求出总记录数
$pst = $conn->prepare('select count(*) from t_user');
$pst->execute();
$result = $pst->fetchAll(PDO::FETCH_NUM);
$recordcount = $result[0][0];

//(2)设置每页多少，第几页，计算总页数
$pagesize = 2;
$currpage = isset($_GET['p']) ? $_GET['p'] : 1;
$pagecount = ceil($recordcount / $pagesize);
if ($currpage < 1) $currpage = 1;
if ($currpage > $pagecount) $currpage = $pagecount;

//(3)查询指定页
$st = $conn->prepare('select * from t_user limit :start,:size');
$start = $currpage * $pagesize - $pagesize;
$size = $pagesize;
$st->bindParam('start', $start, PDO::PARAM_INT);
$st->bindParam('size', $size, PDO::PARAM_INT);
$st->execute();
$rs = $st->fetchAll();
echo '<pre>';
print_r($rs);
echo '<br>';
printf("<a href='?p=%d'>首页</a>&nbsp;", 1);
printf("<a href='?p=%d'>下页</a>&nbsp;", $currpage + 1);
printf("<a href='?p=%d'>上页</a>&nbsp;", $currpage - 1);
printf("<a href='?p=%d'>末页</a>&nbsp;", $pagecount);
echo '<br>';
printf("[第%d页/共%d页][每页%d条/共%d条]", $currpage, $pagecount, $pagesize, $recordcount);